//
// Created by juno on 2021/9/7.
//

#ifndef WEBCHART_ROOM_WEBSOCKET_H
#define WEBCHART_ROOM_WEBSOCKET_H

#include <libwebsockets.h>
#include <stdbool.h>
#include <pthread.h>
#include "member.h"

struct per_session_data {
    struct per_session_data *list;
    struct lws *wsi;
    bool publishing;
    uint32_t tx_tail;
    uint32_t rx_tail;
    struct lws_ring *tx_ring;
    struct lws_ring *rx_ring;
    pthread_mutex_t tx_lock;
    pthread_mutex_t rx_lock;
    char *token;
    struct member_t *member;
};

struct msg {
    unsigned char *payload;
    size_t len;
};

struct per_host_data {
    struct lws_context *ctx;
    struct lws_vhost *host;
    const struct lws_protocols *proto;
    pthread_t pthread_spam[1];
    pthread_mutex_t lock;
    struct per_session_data *list;
    const char *config;
    char finished;
};

struct session_context {
    struct per_host_data *host;
    struct per_session_data *session;
    struct lws *wsi;
};

int ws_server_msg_cb(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len);

#endif //WEBCHART_ROOM_WEBSOCKET_H
